import re
from typing import List

"""
Author: Gaoqiang Du
Description：将markdown内某些数据转换为api可识别数据
Time: 2022/4/17 21:30
"""


default_types = {"string", "boolean", "integer", "object", "array", "null"}


def format_type(type_str: str):
    type_str = type_str.lower()
    if type_str not in default_types:
        if re.search("int", type_str):
            type_str = "integer"
        elif re.search("bool", type_str):
            type_str = "boolean"
        elif re.search("list", type_str):
            type_str = "array"
        elif re.search("dict|map", type_str):
            type_str = "object"
        else:
            type_str = "string"

    return type_str


def format_table_headers(headers: List[str]) -> List[str]:
    format_headers = []
    for i in headers:
        if re.search("名称", i):
            format_headers.append("name")
        elif re.search("类型", i):
            format_headers.append("type")
        elif re.search("必选|必须", i):
            format_headers.append("required")
        elif re.search("示例", i):
            format_headers.append("sampleValue")
        elif re.search("描述|说明|备注", i):
            format_headers.append("description")
        else:
            format_headers.append(i)
    return format_headers


def format_param_item(param_item: List[str], format_headers: List[str]) -> dict:
    format_item = {}
    for item_value, key in zip(param_item, format_headers):
        if key == "required":
            item_value = bool(re.search("是|true", item_value.lower()))
        elif key == "type":
            item_value = format_type(item_value)
        format_item[key] = item_value

    return format_item


def format_res_item(param_item: List[str], format_headers: List[str]) -> dict:
    format_res = {}
    for item_value, key in zip(param_item, format_headers):
        if key == "type":
            item_value = format_type(item_value)
        if key == "sampleValue":
            # format_res["mock"] = {"mock": item_value}
            ...
        else:
            format_res[key] = item_value

    return format_res
